package cn.net.susan.service.seckill;

import cn.net.susan.entity.ResponsePageEntity;
import cn.net.susan.entity.mall.ProductConditionEntity;
import cn.net.susan.entity.mall.ProductEntity;
import cn.net.susan.entity.seckill.SeckillProductConditionEntity;
import cn.net.susan.entity.seckill.SeckillProductEntity;
import cn.net.susan.mapper.BaseMapper;
import cn.net.susan.mapper.mall.ProductMapper;
import cn.net.susan.mapper.seckill.SeckillProductMapper;
import cn.net.susan.service.BaseService;
import cn.net.susan.service.mall.ProductService;
import cn.net.susan.util.AssertUtil;
import cn.net.susan.util.FillUserUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 秒杀商品 服务层
 *
 * @author 苏三 该项目是知识星球：java突击队 的内部项目
 * @date 2024-07-08 10:57:31
 */
@Service
public class SeckillProductService extends BaseService<SeckillProductEntity, SeckillProductConditionEntity> {

    @Autowired
    private SeckillProductMapper seckillProductMapper;
    @Autowired
    private ProductService productService;
    @Autowired
    private ProductMapper productMapper;

    /**
     * 查询秒杀商品信息
     *
     * @param id 秒杀商品ID
     * @return 秒杀商品信息
     */
    public SeckillProductEntity findById(Long id) {
        return seckillProductMapper.findById(id);
    }

    /**
     * 根据条件分页查询秒杀商品列表
     *
     * @param seckillProductConditionEntity 秒杀商品信息
     * @return 秒杀商品集合
     */
    public ResponsePageEntity<SeckillProductEntity> searchByPage(SeckillProductConditionEntity seckillProductConditionEntity) {
        ResponsePageEntity<SeckillProductEntity> responsePageEntity = super.searchByPage(seckillProductConditionEntity);
        fillProduct(seckillProductConditionEntity, responsePageEntity.getData());
        return responsePageEntity;
    }

    private void fillProduct(SeckillProductConditionEntity seckillProductConditionEntity, List<SeckillProductEntity> list) {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }

        List<Long> productIdList = list.stream().map(SeckillProductEntity::getProductId).distinct().collect(Collectors.toList());
        ProductConditionEntity productConditionEntity = new ProductConditionEntity();
        productConditionEntity.setIdList(productIdList);
        productConditionEntity.setPageSize(seckillProductConditionEntity.getPageSize());
        ResponsePageEntity<ProductEntity> responsePageEntity = productService.searchByPage(productConditionEntity);
        if (Objects.nonNull(responsePageEntity) && CollectionUtils.isNotEmpty(responsePageEntity.getData())) {
            for (SeckillProductEntity seckillProductEntity : list) {
                Optional<ProductEntity> optional = responsePageEntity.getData()
                        .stream().filter(x -> x.getId().equals(seckillProductEntity.getProductId())).findAny();
                if (optional.isPresent()) {
                    ProductEntity productEntity = optional.get();
                    seckillProductEntity.setName(productEntity.getName());
                    seckillProductEntity.setModel(productEntity.getModel());
                    seckillProductEntity.setCategoryName(productEntity.getCategoryName());
                    seckillProductEntity.setBrandName(productEntity.getBrandName());
                    seckillProductEntity.setUnitName(productEntity.getUnitName());
                    seckillProductEntity.setCostPrice(productEntity.getPrice());
                }
            }
        }

    }

    /**
     * 新增秒杀商品
     *
     * @param seckillProductEntity 秒杀商品信息
     * @return 结果
     */
    public int insert(SeckillProductEntity seckillProductEntity) {
        checkParam(seckillProductEntity);
        return seckillProductMapper.insert(seckillProductEntity);
    }

    private void checkParam(SeckillProductEntity seckillProductEntity) {
        ProductEntity productEntity = productMapper.findById(seckillProductEntity.getProductId());
        AssertUtil.notNull(productEntity, "商品ID在系统中不存在");
    }

    /**
     * 修改秒杀商品
     *
     * @param seckillProductEntity 秒杀商品信息
     * @return 结果
     */
    public int update(SeckillProductEntity seckillProductEntity) {
		checkParam(seckillProductEntity);
		FillUserUtil.fillUpdateUserInfo(seckillProductEntity);
        return seckillProductMapper.update(seckillProductEntity);
    }

    /**
     * 批量删除秒杀商品对象
     *
     * @param ids 系统ID集合
     * @return 结果
     */
    public int deleteByIds(List<Long> ids) {
        List<SeckillProductEntity> entities = seckillProductMapper.findByIds(ids);
        AssertUtil.notEmpty(entities, "秒杀商品已被删除");

        SeckillProductEntity entity = new SeckillProductEntity();
        FillUserUtil.fillUpdateUserInfo(entity);
        return seckillProductMapper.deleteByIds(ids, entity);
    }

    @Override
    protected BaseMapper getBaseMapper() {
        return seckillProductMapper;
    }

}
